# 388 中等：文件的最长绝对路径
from collections import deque


def lengthLongestPath(input: str) -> int:
    if input.find('.') == -1:
        return 0

    maxm = 0
    path = deque()
    num = deque([-1])
    cnt = 0
    length = 0

    split = input.split('\n')
    for p in split:

        while p.find('\t') != -1:
            cnt += 1
            p = p[1:]

        while cnt <= num.__getitem__(-1):
            temp = path.pop()
            num.pop()
            length -= len(temp)

        length += len(p)
        if p.find('.') > -1:
            maxm = max(maxm, length + cnt)

        path.append(p)
        num.append(cnt)
        cnt = 0

        print(path, num, length, maxm)

    return maxm


print(lengthLongestPath('dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext'))
print(lengthLongestPath('file1.txt\nfile2.txt\nlongfile.txt'))
print(lengthLongestPath('a'))
print(lengthLongestPath("a\n\taa\n\t\taaa\n\t\t\tfile1234567890123.txt\naaaaaaaaaaaaaaaaaaaaa\n\tsth.png"))
print("a\n\taa\n\t\taaa\n\t\t\tfile1234567890123.txt\naaaaaaaaaaaaaaaaaaaaa\n\tsth.png")


# class Solution:
#     def lengthLongestPath(self, input: str) -> int:
#         st = []
#         ans, i, n = 0, 0, len(input)
#         while i < n:
#             # 检测当前文件的深度
#             depth = 1
#             while i < n and input[i] == '\t':
#                 depth += 1
#                 i += 1
#
#             # 统计当前文件名的长度
#             length, isFile = 0, False
#             while i < n and input[i] != '\n':
#                 if input[i] == '.':
#                     isFile = True
#                 length += 1
#                 i += 1
#             i += 1  # 跳过换行符
#
#             while len(st) >= depth:
#                 st.pop()
#             if st:
#                 length += st[-1] + 1
#             if isFile:
#                 ans = max(ans, length)
#             else:
#                 st.append(length)
#         return ans
